<?php

Class AnswersController extends Controller{
    function actionAnswers($id = null){
        $answer = new Answers();
        $question = new Questions();
        
        $model = new Answers();
        $criteria = new CDbCriteria();
        $criteria->compare("ques_id", $id);
        
        $ansReport = new CActiveDataProvider($model, array(
            "criteria" => $criteria
        ));
        
        $choice = Choice::model()->findByPk($id);
    //    $ansReport = new CActiveDataProvider($model);
        $aa=array();
      for($i=1;$i<=5;$i++){
      $cntCriteria = new CDbCriteria();
      $ch = "choice".$i;
     // echo "ch == $ch<br/>";
     $cntCriteria->condition = "ques_id = :ques_id";
     $cntCriteria->addCondition("ans_name = :ans_name");
     $cntCriteria->params[':ques_id'] = $id;
     $cntCriteria->params[':ans_name'] = $choice[$ch];
      $articleCount = Answers::model()->count($cntCriteria);
      $aa[] = $articleCount;
     // echo "articleCount == $articleCount<br/>";
      }
     // print_r($aa);
        
        //save
        if(!empty($id)){
            $ques = Questions::model()->findByPk($id);
        }
        
        $this->render("Answers", array(
            "ques" => $ques,
            "aa" => $aa,
            "choice" => $choice,
        ));
    }

    function actionResultGrap() {
        $model = new Answers();
        $answers = new CActiveDataProvider($model);

        $this->render("ResultGrap", array(
            "answers" => $answers
        ));
    }

    function actionAddPoll() {
        $answers = new Answers();
        $comment = new Comments();
        
        if (!empty($_POST)) {
           //echo "1"; 
            $answers->ans_name = $_POST["poll"];
            //echo $_POST["poll"];
            $answers->ques_id = $_POST["quesId"];
            //echo $_POST["quesId"];
            $answers->id = Yii::app()->session["user_id"];
            //echo Yii::app()->session["user_id"];
            $answers->ans_created = date('Y-m-d H:i:s');

            $comment->ment_name = $_POST["message"];
            $comment->ques_id = $_POST["quesId"];
            $comment->id = Yii::app()->session["user_id"];
            $comment->ment_created = date('Y-m-d H:i:s');
            
            if ($answers->save()) {
                $comment->save();
                $this->redirect(array("//Questions/Index"));
              
            }
        }

        $this->render("answers", array(
            "answers" => $answers
        ));
    }
    
    public function actionViewComment($id=null) {
        
        $comments = new Comments();
        $criteria = new CDbCriteria();
        $criteria->compare("ques_id", $id);
        $criteria->order = "ment_id DESC";
        
        $comment = new CActiveDataProvider($comments, array(
            "criteria" => $criteria
        ));
        
        //$ans = new Answers();
        
        
        $this->render("ViewComment", array(
            "comment" => $comment
        ));
    }
  
}

?>
